package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.Exception.MyException;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.ExaminationReport;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckResultService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author ： ZAG WELL
 * @Date : 2020/12/7
 */
@RestController
@RequestMapping("/checkresult")
public class CheckResultController {

    @Reference
    private CheckResultService checkResultService;

    @PostMapping("/updateAdvice")
    public Result updateAdvice(@RequestBody ExaminationReport examinationReport) {
        checkResultService.updateAdvice(examinationReport);
        return new Result(true, "添加医嘱成功");
    }

    @GetMapping("/getExaminationReport")
    public Result getExaminationReport(@RequestParam("id") Integer id) {
        System.out.println(id);
        ExaminationReport examinationReport = checkResultService.getExaminationReport(id);
        return new Result(true, "获取检查报告成功", examinationReport);
    }

    @PostMapping("/uploadedReport")
    public Result uploadedReport(@RequestBody ExaminationReport examinationReport) {
//        获取当前登录用户 用于填写导出的体检报告检查医师处
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
//        上传文件
        try {
            checkResultService.uplodedAndSendEmail(examinationReport,username);
        } catch (IOException e) {
            throw new MyException("上传期间发生异常");
        }
        return new Result(true, "上传检查报告成功，即将通过邮件发送体检报告给会员");
    }

}
